package com.example.wechatappbackend.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wechatappbackend.VO.DishVO;
import com.example.wechatappbackend.VO.MeuaVO;
import com.example.wechatappbackend.common.CommonResponse;
import com.example.wechatappbackend.common.JsonMsg;
import com.example.wechatappbackend.entity.Dish;
import com.example.wechatappbackend.entity.DishSpecies;
import com.example.wechatappbackend.persistence.DishMapper;
import com.example.wechatappbackend.persistence.DishSpeciesMapper;
import com.example.wechatappbackend.service.DishService;
import com.example.wechatappbackend.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.example.wechatappbackend.utils.Utils.dishToDishVO;

@Service("dishService")
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishSpeciesMapper dishSpeciesMapper;

    @Override
    public CommonResponse<List<DishSpecies>> getDishList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("species","1");
        List<DishSpecies> dishSpeciesList = dishSpeciesMapper.selectList(queryWrapper);

        if(dishSpeciesList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有分类信息");
        }
        return CommonResponse.createForSuccess(dishSpeciesList);
    }

    @Override
    public CommonResponse<List<Dish>> getDishListByDishSpecies(String speciesId) {
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("species", speciesId);
        List<Dish> dishList = dishMapper.selectList(queryWrapper);
        if (dishList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("该speciesId对应的没有dish");
        }
        return CommonResponse.createForSuccess(dishList);
    }

    @Override
    public JsonMsg getAllDish() {



        List<DishSpecies> dishSpeciesList = dishSpeciesMapper.selectList(null);

        List<MeuaVO> meuaVOList = new ArrayList<>();

        List<DishVO> dishVOList[] = new ArrayList[dishSpeciesList.size()+1];

        for(int i = 0 ;i < dishSpeciesList.size();i++){

            int speciesId = dishSpeciesList.get(i).getSpecies();
            String name = dishSpeciesList.get(i).getSpeciesDesc();

            QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("categoryID", speciesId).eq("status",1);

            DishSpecies dishSpecies = dishSpeciesMapper.selectById(speciesId);
            List<Dish> dishList = dishMapper.selectList(queryWrapper);

            dishVOList[i] = new ArrayList<>();

            for (Dish dish : dishList) {
                DishVO dishVO = dishToDishVO(dish, dishSpecies);
                dishVOList[i].add(dishVO);

            }

            MeuaVO meuaVO = Utils.createMeua(name,dishVOList[i]);

            meuaVOList.add(meuaVO);
//            dishVOList.clear();
        }

        return new JsonMsg("0","SUCCESS",meuaVOList);
    }

}
